<?php


namespace app\admin\controller;

use app\admin\model\User;
use app\common\BaseController;
use think\Request;

/**
 * @Apidoc\Title("配置管理")
 **/
class Settings extends BaseController
{

    public function index(){
        /*配置列表*/
		$group_list = [
            'site_info'    => '基础信息',
            'other'        => '其他',
            'basic'        => '其他',
            'merchant'     => '商户'
        ];	
        $inc_type =  request()->get('inc_type','site_info');
        $config   =  freshCache($inc_type);
        return $this->success('', compact('inc_type','group_list','config'));
    }

        /**
     * @name: 设置提交
     * @Author: Sam
     * @Date: 2020-08-31 14:13:01
     * @msg: 
     * @param {type} 
     * @return {type} 
     */
    public function set_handle(){
        $data     = input('post.');
        $inc_type = $data['inc_type'];
        $data     = $data[$inc_type];
        unset($data['inc_type']);
        freshCache($inc_type,$data);
        return json_response(1,'操作成功!');
    }
        /**
     * @Apidoc\Title ("详情")
     * @Apidoc\Url("user/user/:id")
     * @Apidoc\Method("GET")
     * @Apidoc\Tag("account")
     * @Apidoc\Param("id", type="string", require=true, desc="账号id")
     * @Apidoc\Returned("data", type="array", desc="业务数据",replaceGlobal="true",
     *   @Apidoc\Returned("data",type="string",desc="账号信息")
     * )
     */
    public function detail(int $id): \think\response\Json
    {
        try {
            $row  = User::where('id', $id)->find();
        } catch (DataNotFoundException | ModelNotFoundException | DbException $e) {
            return $this->error($e->getMessage());
        }

        return $this->success('', $row);
    }


        /**
     * @Apidoc\Title("创建")
     * @Apidoc\Url("user")
     * @Apidoc\Method("POST")
     * @Apidoc\Tag("account")
     * @Apidoc\Param("nickname", type="string", require=false, desc="昵称")
     * @Apidoc\Param("mobile", type="string", require=false, desc="头像")
     * @Apidoc\Param("mobile", type="string", require=false, desc="手机号码")
     */
    public function create(Request $request): \think\response\Json
    {
        $data = $request->post();
        try {
            $this->validate($data, [
                'nickname' => 'require',
            ], [
                'nickname.require'  => '昵称必须'
            ]);
        } catch (ValidateException $e) {
            return $this->error($e->getError());
        }
        $result = User::create([
            'nickname'  => $data['nickname'],
            'mobile' => $data['mobile'],
            'avatar' => $data['avatar'],
            'openid' => $data['openid']
        ]);
        if ($result->isEmpty()) {
            return $this->error('错误');
        }

        return $this->success('成功');
    }


            /**
     * @Apidoc\Title("创建")
     * @Apidoc\Url("user")
     * @Apidoc\Method("POST")
     * @Apidoc\Tag("account")
     * @Apidoc\Param("nickname", type="string", require=false, desc="昵称")
     * @Apidoc\Param("mobile", type="string", require=false, desc="头像")
     * @Apidoc\Param("mobile", type="string", require=false, desc="手机号码")
     */
public function edit(Request $request): \think\response\Json
    {
        $data = $request->post();
        try {
            $this->validate($data, [
                'nickname' => 'require',
                'id'       => 'require',
            ], [
                'nickname.require'  => '昵称必须',
                'id.require'  => '主键必须'
            ]);
        } catch (\Exception $e) {
            return $this->error($e->getError());
        }
        $result = User::update([
            'nickname'  => $data['nickname'],
            'mobile' => $data['mobile'],
            'avatar' => $data['avatar'],
            'openid' => $data['openid']
        ],['id'=>$data['id']]);
        if ($result->isEmpty()) {
            return $this->error('错误');
        }

        return $this->success('成功');
    }
}
